import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class gethref {
	public static List<String> getImgStr(String htmlStr) {
		String img = "";
		Pattern p_image;
		Matcher m_image;
		List<String> pics = new ArrayList<String>();

		// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址

		String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		m_image = p_image.matcher(htmlStr);
		while (m_image.find()) {
			img = img + "," + m_image.group();
			// Matcher m =
			// Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src

			Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);

			while (m.find()) {
				pics.add(m.group(1));
			}
		}
		return pics;
	}

	public static String read() {
		File file = new File("C:\\j2ee\\新建文本文档.html");
		BufferedReader reader = null;
		StringBuffer buff = new StringBuffer();
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;

			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				buff.append(tempString + "/r/n");
				// System.out.println("line " + line + ": " + tempString);
				line++;
			}
			// System.out.println(buff.toString());
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return buff.toString();
	}

	public static void main(String[] args) {

		Pattern pattern = Pattern.compile("<a\\s+[^<>]*\\s+href=\"([^<>\"]*)\"[^<>]*>");
		List<String> href = new ArrayList<String>();

		Matcher matcher = pattern.matcher(read());
		while (matcher.find()) {
			href.add(matcher.group(1));
		}
		href.forEach(System.out::println);

		
		Pattern pattern1 = Pattern.compile("<a[^>]*>([^<]*)</a>");
		List<String> title = new ArrayList<String>();
		Matcher matcher1 = pattern1.matcher(read());
		while (matcher1.find()) {
			title.add(matcher1.group(1));
		}
		title.forEach(System.out::println);

		

	}
}
